package com.studyweb.controller;

import com.studyweb.pojo.entity.WritingMaterialAccumulation;
import com.studyweb.pojo.vo.Result;
import com.studyweb.service.WritingMaterialAccumulationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/writing-material-accumulation")
@CrossOrigin
public class WritingMaterialAccumulationController {
    
    @Autowired
    private WritingMaterialAccumulationService writingMaterialAccumulationService;
    
    /**
     * 获取所有作文素材积累列表
     * @return 作文素材积累列表
     */
    @GetMapping("/list")
    public Result<List<WritingMaterialAccumulation>> list() {
        try {
            List<WritingMaterialAccumulation> list = writingMaterialAccumulationService.list();
            return Result.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取作文素材积累列表失败");
        }
    }
    
    /**
     * 根据ID获取作文素材积累详情
     * @param id 主键ID
     * @return 作文素材积累详情
     */
    @GetMapping("/{id}")
    public Result<WritingMaterialAccumulation> getById(@PathVariable Long id) {
        try {
            WritingMaterialAccumulation writingMaterialAccumulation = writingMaterialAccumulationService.getById(id);
            if (writingMaterialAccumulation != null) {
                return Result.success(writingMaterialAccumulation);
            } else {
                return Result.error("未找到对应的作文素材积累内容");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取作文素材积累详情失败");
        }
    }
    
    /**
     * 新增作文素材积累
     * @param writingMaterialAccumulation 作文素材积累对象
     * @return 操作结果
     */
    @PostMapping("/add")
    public Result<String> add(@RequestBody WritingMaterialAccumulation writingMaterialAccumulation) {
        try {
            writingMaterialAccumulationService.add(writingMaterialAccumulation);
            return Result.success("新增作文素材积累成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("新增作文素材积累失败");
        }
    }
    
    /**
     * 更新作文素材积累
     * @param writingMaterialAccumulation 作文素材积累对象
     * @return 操作结果
     */
    @PutMapping("/update")
    public Result<String> update(@RequestBody WritingMaterialAccumulation writingMaterialAccumulation) {
        try {
            writingMaterialAccumulationService.update(writingMaterialAccumulation);
            return Result.success("更新作文素材积累成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新作文素材积累失败");
        }
    }
    
    /**
     * 删除作文素材积累
     * @param id 主键ID
     * @return 操作结果
     */
    @DeleteMapping("/delete/{id}")
    public Result<String> delete(@PathVariable Long id) {
        try {
            writingMaterialAccumulationService.delete(id);
            return Result.success("删除作文素材积累成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除作文素材积累失败");
        }
    }
    
    /**
     * 批量删除作文素材积累
     * @param ids 主键ID列表
     * @return 操作结果
     */
    @DeleteMapping("/delete/batch")
    public Result<String> batchDelete(@RequestBody List<Long> ids) {
        try {
            writingMaterialAccumulationService.batchDelete(ids);
            return Result.success("批量删除作文素材积累成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("批量删除作文素材积累失败");
        }
    }
    
    /**
     * 搜索作文素材积累
     * @param keyword 关键词
     * @return 搜索结果
     */
    @GetMapping("/search")
    public Result<List<WritingMaterialAccumulation>> search(@RequestParam String keyword) {
        try {
            List<WritingMaterialAccumulation> list = writingMaterialAccumulationService.search(keyword);
            return Result.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("搜索作文素材积累失败");
        }
    }
}